# --- building --- 
#FROM harbor.xhhk/common/pyinstaller:py37-alpine AS builder
#FROM ubuntu:18.04 AS builder
FROM harbor.xhhk/common/pyinstaller-pandas:py37-ubuntu AS builder


ARG APP_NAME="a1001"
ARG APP_FILE="service.py"


ADD requirements.txt /data/
ADD lycium-0.0.15-py3-none-any.whl /data/
RUN pip3 --no-cache-dir install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com -r /data/requirements.txt

RUN mkdir -p /app/${APP_NAME}
WORKDIR /app/${APP_NAME}

COPY . ./


RUN pyinstaller --name=${APP_NAME} --clean --key $(openssl rand -base64 32) $APP_FILE

RUN cp -R ./etc/ dist/${APP_NAME}

RUN ls -la dist/${APP_NAME}/

RUN du -sh dist/


# --- producting ---
#FROM ubuntu:18.04
FROM harbor.xhhk/common/pandas-producting-base:ubuntu-1804-producting-base

ARG APP_NAME="a1001"
ENV APP_NAME=${APP_NAME}

RUN mkdir -p /app/${APP_NAME}
WORKDIR /app/${APP_NAME}
COPY --from=builder /app/${APP_NAME}/dist/${APP_NAME} ./
RUN mkdir -p /app/${APP_NAME}/log

#RUN ls -la /app/${APP_NAME}

RUN  echo "#!/bin/sh\nwaittimes=0\nuntil nc -w 1 localhost 15000\ndo\n    echo '>>> Waiting for sidecar'\n    waittimes=\$((waittimes + 1))\n    if [ \${waittimes} -gt 10 ]; then break; fi\n    sleep 2\ndone\nsleep 5\necho '>>> Starting application...'\n" > /usr/local/bin/entrypoint.sh


RUN echo  "\n$@\n./${APP_NAME}\n" >> /usr/local/bin/entrypoint.sh \
    && chmod +x /usr/local/bin/entrypoint.sh


# ubuntu add group and user
RUN groupadd -r xhhk -g 2020 && useradd -r xhhk -u 2020 -g xhhk

RUN chown 2020:2020 -R /app/${APP_NAME} && chown 2020:2020 /usr/local/bin/entrypoint.sh
USER 2020
WORKDIR /app/${APP_NAME}

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
